package com.open.system.service.impl;

import com.open.system.mapper.RoleMapper;
import com.open.common.model.Page;
import com.open.base.model.Role;
import com.open.common.model.Sort;
import com.open.system.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色信息
 *
 * @author 大树03
 * @date 2025-02-01 22:55
 */
@Service(value = "roleService")
@Slf4j
public class RoleServiceImpl implements IRoleService {

	private RoleMapper roleMapper;

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    @Override
    @Transactional()
    public Boolean save(Role model)  {
        return roleMapper.save(model);
    }

    @Override
    @Transactional()
    public Boolean update(Role model)  {
        return roleMapper.update(model);
    }

    @Override
    @Transactional()
    public Boolean batchSave(List<Role> models) {
        return roleMapper.batchSave(models);
    }

    @Override
    @Transactional()
    public Boolean batchUpdate(List<Role> models) {
        return roleMapper.batchUpdate(models);
    }

	@Override
	public Page<Role> findPage(Page<Role> page,Role item)  {
        Map<String,Object> map = new HashMap<String, Object>(4);
        int startPoint = (page.getIndex()-1)*page.getSize();
        map.put("orderBy",new Sort());
        map.put("item",item);
        map.put("startPoint",startPoint);
                map.put("endPoint",page.getSize());
                Integer count = roleMapper.findPageCount(map);
        if (count > startPoint) {
            page.setItems(roleMapper.findPage(map));
        }else {
            page.setItems(new ArrayList<Role>());
        }
        page.setTotal(count);
        return page;
    }

	@Override
    public List<Role> findByModel(Role item)  {
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return roleMapper.findByModel(map);
    }

	@Override
	public Role findByPk(Long pk)  {
        return roleMapper.findByPk(pk);
    }

    @Override
    public Integer countByModel(Role item){
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return roleMapper.findPageCount(map);
    }

    @Override
    @Transactional()
    public Boolean remove(List<Long> pks) {
        for (Long pk : pks) {
            roleMapper.remove(pk);
        }
        return true;
    }

    @Override
    public List<Role> findByUserId(Long userId) {
        return roleMapper.findByUserId(userId);
    }
}